home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / vdit.arc / loadfont.c < prev    next >
C/C++ Source or Header  |  1988-07-12  |  3KB  |  125 lines

  1. /*
  2.  * Load GDOS font, Eric Gisin.
  3.  * loads a font from file name.FED. Tries in ., then $FONTDIR, then A:\FONT.
  4.  * does not have enough error checking.
  5.  */
  6.  
  7. #define    NULL    0L
  8. #include <gfont.h>
  9.  
  10. #define    open(file, mode)    gemdos(0x3D, file,mode)
  11. #define close(fd)        gemdos(0x3E, fd)
  12. #define    read(fd, buf, size)    gemdos(0x3F, fd,(long)size,buf)
  13.  
  14. #define    Swapw(v)    (v) = ((v)<<8) | ((v)>>8)&0x00FF
  15. #define    Swapl(v)    (v) = ((v)>>24)&0x000000FF | ((v)>>8)&0x0000FF00
  16.  
  17. char * font_dir = NULL;
  18. extern char * malloc();
  19. extern char * getenv();
  20.  
  21. /*
  22.  * load GDOS font, return pointer to font.
  23.  */
  24. GFont *
  25. load_font(name)
  26.     char *    name;
  27. {
  28.     int fd, i, n, size;
  29.     char temp [64], * p = temp;
  30.     char * q, * tcwd;
  31.     int swapf;
  32.     register GFont*    fp;
  33.  
  34.     if (font_dir == NULL)
  35.         font_dir = getenv("FONTDIR");
  36.     if (font_dir == NULL)
  37.         font_dir = "A:\\font";
  38.  
  39.     fp = malloc(sizeof(GFont));
  40.     if (fp == NULL) return NULL;
  41.  
  42.     /* set up font file's path name */
  43.     for (q = font_dir; *q; )
  44.         *p++ = *q++;
  45.     *p++ = '\\';
  46.     tcwd = p;
  47.     for (q = name; *q; )
  48.         *p++ = *q++;
  49.     for (q = ".fed"; *q; )
  50.         *p++ = *q++;
  51.     *p = '\0';
  52.  
  53.     /* open font file */
  54.     fd = open(tcwd, 0);        /* try name.fed */
  55.     if (fd < 0)
  56.         fd = open(temp, 0);
  57.     if (fd < 0)
  58.         return (0);
  59.  
  60.     /* read header */
  61.     size = sizeof(GFont);
  62.     read(fd, fp, size);        /* should (char *)fp */
  63.  
  64.     swapf = !(fp->flags&0x0004);
  65.     if (swapf) {
  66.         /* swap header words and longs */
  67.         Swapw(fp->font_id);        Swapw(fp->size);
  68.         Swapw(fp->first_ade);        Swapw(fp->last_ade);
  69.         Swapw(fp->top);            Swapw(fp->ascent);
  70.         Swapw(fp->half);        Swapw(fp->descent);
  71.         Swapw(fp->bottom);
  72.         Swapw(fp->max_char_width);    Swapw(fp->max_cell_width);
  73.         Swapw(fp->loffset);        Swapw(fp->roffset);
  74.         Swapw(fp->thicken);        Swapw(fp->ul_size);
  75.         /* Swapw(fp->flags); */        Swapl(fp->hoff_base);
  76.         Swapl(fp->coff_base);        Swapl(fp->form_base);
  77.         Swapw(fp->form_width);        Swapw(fp->form_height);
  78.     }
  79.  
  80.     /* read offset table */
  81.     n = fp->last_ade + 1 - fp->first_ade + 1;
  82.     size = n * sizeof(short);
  83.     fp->coff_base = malloc(size);
  84.     if (fp->coff_base == NULL) return NULL;
  85. #if 0
  86.     for (i = 0; i < fp->first_ade; i++)
  87.         fp->coff_base[i] = 0;
  88. #endif
  89.     read(fd, fp->coff_base, size);
  90.     if (swapf)        /* swap offset array words */
  91.         for (i = 0; i < n; i++)
  92.             Swapw(fp->coff_base[i]);
  93.  
  94.     /* read bitmap */
  95.     size = fp->form_width*fp->form_height;
  96.     fp->form_base = malloc(size);
  97.     if (fp->form_base == NULL) return NULL;
  98.     read(fd, fp->form_base, size);
  99.  
  100.     close(fd);
  101.     return fp;
  102. }
  103.  
  104. /* load a set of fonts, linking them for vst_load_font */
  105. GFont *
  106. load_fonts(fontv)
  107.     char **fontv;
  108. {
  109.     int errors = 0;            /* should return */
  110.     GFont * fhead = NULL;
  111.     GFont * f, ** fpp = &fhead;
  112.  
  113.     /* load fonts, create linked list fhead */
  114.     while (*fontv != NULL) {
  115.         *fpp = f = load_font(*fontv++);
  116.         if (f != NULL)
  117.             fpp = &f->next_font;
  118.         else
  119.             errors++;
  120.     }
  121.     return fhead;
  122. }
  123.  
  124.  
  125.